package com.kailong.servlet.student;

import com.kailong.dao.admin.ManageStudentDao;
import com.kailong.dao.student.SelectStudentDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/student/doChangePwd")
public class DoChangePwd extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        String sno = (String) session.getAttribute("sno");
        String pwdnow = req.getParameter("pwdnow");
        String pwdnew = req.getParameter("pwdnew");
        int count = SelectStudentDao.selectBySno(sno,pwdnow);
        PrintWriter out = resp.getWriter();
        if (count>0){
            int pwdChange = ManageStudentDao.updatePwd(sno,pwdnew);
            if (pwdChange>0) {
                out.print("<script>");
                out.print("alert('密码修改成功！');");
                out.print("location.href='http://localhost:8080/Library/student/studentChangePwd.jsp';");
                out.print("</script>");
            }else {
                out.print("<script>");
                out.print("alert('密码修改失败！详情咨询管理员!');");
                out.print("location.href='http://localhost:8080/Library/student/studentChangePwd.jsp';");
                out.print("</script>");
            }
        }else {
            out.print("<script>");
            out.print("alert('您输入的现有密码不对！');");
            out.print("location.href='http://localhost:8080/Library/student/studentChangePwd.jsp';");
            out.print("</script>");
        }
    }
}
